<?php

	include_once("./db.php"); 
	
	$userId = $_SESSION["user"]["Id"];

	$modeId = $_REQUEST['mode'];

	if($modeId)
		$modeId = "um.ModeId = ". quote_smart($modeId) . " AND ";

	$res = db_select("SELECT 
		e.Id, 
		e.Name 
		FROM
			Element e,
			ElementMode em,
			UserMode um, 
			UserElement aa
		WHERE
			e.Id = em.ElementId  AND
			em.ModeId = um.ModeId AND
			um.UserId = $userId AND
			$modeId
			(Starter
			OR EXISTS (SELECT 1 FROM UserElement ue WHERE ue.UserId = $userId AND ue.ElementId = e.Id))");

	while($item = mysql_fetch_array($res, MYSQL_ASSOC))
		$ret["Elements"][] = $item;
	
	echo json_result_encode($ret);

?>